/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.model.command;

import static com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil.getFragments;
import static com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil.getOperands;

import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.uml2.uml.CombinedFragment;
import org.eclipse.uml2.uml.InteractionFragment;
import org.eclipse.uml2.uml.InteractionOperand;

/**
 * This command takes a CombinedFragment and moves all of its contained
 * fragments to its owner. The command is equal to SetOwnerCommand an all
 * contained fragments.
 * 
 * <p>
 * The following attributes must be set for the command to complete
 * successfully:
 * </p>
 * <ul>
 * <li>CombinedFragment
 * </ul>
 * 
 * @author frankd@ifi.uio.no
 */
public class ReleaseContainedFragmentsCommand extends CompoundCommand {

	// We implement this command in terms of SetOwnerCommand, which
	// provides complete undo. The problem is that we don't know
	// initially how many interaction fragments are affected and we
	// we don't want to allocate the SetOwnerCommands until it is
	// really necessary - possibly the command won't ever be executed.
	// Neither do we want to make any assumptions on how commands
	// are executed - in terms of index into an array or iterators
	// or whatever.
	// 
	// The strategy we have adopted is to reserve one command - a
	// DelegatingCommand - at instantiation time. This command is
	// populated with the real SetOwnerCommands at execution time.
	// 
	// This strategy should not violate any particular design decisions
	// in the Eclipse framework about how commands are executed.

	private CombinedFragment cfragment;
	private DelegatingCommand setOwnerCommands;

	public ReleaseContainedFragmentsCommand() {
		final CompoundCommand cmds = new CompoundCommand();
		add(new Command() {
			public void execute() {
				InteractionFragment newOwner = (cfragment.getEnclosingOperand() != null) ? cfragment
						.getEnclosingOperand()
						: cfragment.getEnclosingInteraction();
				for (InteractionOperand operand : getOperands(cfragment)) {
					for (InteractionFragment fragment : getFragments(operand)) {
						SetOwnerCommand setOwnerCmd = new SetOwnerCommand();
						setOwnerCmd.setFragment(fragment);
						setOwnerCmd.setOwner(newOwner);
						cmds.add(setOwnerCmd);
					}
				}

				setOwnerCommands.setCommand(cmds);
			}
		});

		setOwnerCommands = new DelegatingCommand();
		add(setOwnerCommands);
	}

	public void setCombinedFragment(final CombinedFragment cfragment) {
		this.cfragment = cfragment;
	}

	@Override
	public void dispose() {
		setOwnerCommands = null;
		cfragment = null;
	}
}
